import { getFundpiedata } from '../../../../services/positionStatistics_service';

export default {
  namespace: 'positionStatisticsRule',
  state: {
    fundPieData: {
      title: '',
      date: '',
      items: [],
    },

    fundPieTypeData: {
      title: '',
      date: '',
      items: [],
    },

    fundPieTwoData: {
      title: '',
      date: '',
      items: [],
    },

    fundPieFourData: {
      title: '',
      date: '',
      items: [],
    },
  },

  effects: {
    *GetFundPieData({ payload, callback }, { call, put }) {
      const response = yield call(getFundpiedata, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (payload.fund === '0') {
        yield put({
          type: 'saveFundPieData',
          payload: response,
        });
        if (callback) callback(response);
      } else if (payload.fund === '1') {
        yield put({
          type: 'saveFundPieTypeData',
          payload: response,
        });
        if (callback) callback(response);
      } else if (payload.fund === '2') {
        yield put({
          type: 'saveFundPieTwoData',
          payload: response,
        });
        if (callback) callback(response);
      } else if (payload.fund === '4') {
        yield put({
          type: 'saveFundPieFourData',
          payload: response,
        });
        if (callback) callback(response);
      }
    },
  },

  reducers: {
    saveFundPieData(state, action) {
      return {
        ...state,
        fundPieData: action.payload,
      };
    },

    saveFundPieTypeData(state, action) {
      return {
        ...state,
        fundPieTypeData: action.payload,
      };
    },

    saveFundPieTwoData(state, action) {
      return {
        ...state,
        fundPieTwoData: action.payload,
      };
    },

    saveFundPieFourData(state, action) {
      return {
        ...state,
        fundPieFourData: action.payload,
      };
    },
  },
};
